#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m;
long long a[N],w[N],t[6];
long long c[N];
int k[N],k1[N];
int K;
long long p2[25];
char s[20][140000];
int add[20][140000];
long long ans;
void up(int dep,int u)
{
    // cout<<dep<<" "<<u<<" "<<add[dep][u]<<endl;
    if(dep==0)
    {
        ans+=u+1;
        return;
    }
    if(add[dep][u]==0)
    {
        if(add[dep-1][u<<1]==3)
            add[dep-1][u<<1]=2;
        up(dep-1,u<<1);
        if(add[dep-1][u<<1|1]==3)
        {
            add[dep-1][u<<1|1]=1;
            up(dep-1,u<<1|1);
        }
    }else if(add[dep][u]==1)
    {
        if(add[dep-1][u<<1|1]==3)
            add[dep-1][u<<1|1]=2;
        up(dep-1,u<<1|1);
        if(add[dep-1][u<<1]==3)
        {
            add[dep-1][u<<1]=1;
            up(dep-1,u<<1);
        }
    }else if(add[dep][u]==2)
    {
        if(add[dep-1][u<<1|1]==3)
            add[dep-1][u<<1|1]=2;
        up(dep-1,u<<1|1);
        if(add[dep-1][u<<1]==3)
            add[dep-1][u<<1]=2;
        up(dep-1,u<<1);
    }
}
int win(int dep,int u,int l,int r,int num)
{
    if(l==r)
    {
        return u+1;
    }
    int mid=(l+r)>>1;
    int wl=win(dep-1,u<<1,l,mid,num),wr=win(dep-1,u<<1|1,mid+1,r,num);
    // cout<<wl<<" "<<wr<<endl;
    int winner;
    if(wl>num&&wr>num)
    {
        add[dep][u]=2;
        return wl;
    }else if(wr>num)
    {
        if(s[dep][u]=='0')
        {
            if(w[wl]>=dep)
                winner=wl,add[dep][u]=0;
            else    
                winner=wr,add[dep][u]=1;
        }else
        {
            winner=wl,add[dep][u]=3;
        }
        return winner;
    }
    if(s[dep][u]=='0')
    {
        if(w[wl]>=dep)
            winner=wl,add[dep][u]=0;
        else    
            winner=wr,add[dep][u]=1;
    }else
    {
        if(w[wr]>=dep)
            winner=wr,add[dep][u]=1;
        else
            winner=wl,add[dep][u]=0;
    }
    return winner;
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    for(int i=0,j=1;i<20;i++,j=j*2)
    {
        p2[i]=j;
    }
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&c[i]);
            k[i]=lower_bound(p2,p2+19,c[i])-p2;
            // if(p2[k[i]]==c[i])
            //     k1[i]=k[i];
            // else   
            //     k1[i]=k[i]-1;
        }
        K=lower_bound(p2,p2+19,m)-p2;
        for(int i=1;i<=K;i++)
            scanf("%s",s[i]);
        int T;
        scanf("%d",&T);
        
        for(int o=1;o<=T;o++)
        {
            long long tol=0;
            memset(w,-1,sizeof w);
            for(int i=0;i<4;i++)
                scanf("%lld",&t[i]);
            for(int l=1;l<=m;l++)
            {
                for(int i=1;i<=c[l];i++)
                {
                    w[i]=a[i]^t[i%4];
                }
                win(k[l],0,1,p2[k[l]],c[l]);
                ans=0;
                up(k[l],0);
                tol^=l*ans;
                // puts("");
            }
            printf("%lld\n",tol);
            
        }
    }
    
    
    return 0;
}